package com.jlnx.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;


@WebServlet("/RegistController")
public class RegistController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
				String entNo = request.getParameter("entNo");
				String name = request.getParameter("name");
				String entCodeCredit = request.getParameter("socialCreditCode");
				String password =request.getParameter("password");
				String phoneNo =request.getParameter("phoneNo");
				int role = Integer.parseInt(request.getParameter("role"));
				System.out.println(entNo + ":" + name + ":" + entCodeCredit + ":" + password + ":" + phoneNo + ":" + role);
				
				EnterpriseDaoImpl dao = new EnterpriseDaoImpl();
				ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
				CoreEnterpriseDaoImpl coredao = new CoreEnterpriseDaoImpl();
				
				Enterprise e = new Enterprise(entNo,name, entCodeCredit, password, phoneNo, role);
				if(dao.insertEnterprise(e) < 0) {
					System.out.println("注册失败");
					response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
				}else {
					System.out.println("注册成功");
					if(role == 1) {
						coredao.insertCoreEnterprise(new CoreEnterprise(e));
					}else if(role == 2) {
						
					}else if(role == 3) {
						chaindao.insertChainEnterprise(new ChainEnterprise(e));
					}
					JSONObject responseObject = new JSONObject();
					responseObject.put("code", 200);
					response.setStatus(HttpServletResponse.SC_OK);
					response.getWriter().println(responseObject);
				}
	}

}
